package com.haust.su.ghyy.hospital.controller.api;

import com.haust.su.ghyy.common.exception.YyghException;
import com.haust.su.ghyy.common.helper.HttpRequestHelper;
import com.haust.su.ghyy.common.result.Result;
import com.haust.su.ghyy.common.result.ResultCodeEnum;
import com.haust.su.ghyy.common.utils.MD5;
import com.haust.su.ghyy.hospital.service.HospitalService;
import com.haust.su.ghyy.hospital.service.HospitalSetService;
import com.haust.su.ghyy.model.hosp.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static javafx.scene.input.KeyCode.H;

/**
 * @author haustsusu
 * @create 2023-04-26-12:21
 */

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;


    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode =(String)Parammap.get("hoscode");
        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //如果签名一样继续往下进行,根据hoscode来调用hospitalserveice中的方法
        Hospital hospital=hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);


        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String hoscode=(String)Parammap.get("hoscode");
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
          //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中"+"准换为了" ",因此我们要转换过来
        String logoData = (String)Parammap.get("logoData");
        logoData= logoData.replaceAll(" ", "+");
        Parammap.put("logoData",logoData);



        //调用service中的方法
        hospitalService.save(Parammap);
        return Result.ok();


    }
}
